<template>
  <div class="editable-cell">
    <div v-if="editable" class="editable-cell-input-wrapper">
      <a-input :value="value" @change="handleChange" @pressEnter="check"/>
      <a-icon type="check" class="editable-cell-icon-check" @click="check"/>
    </div>
    <div v-else class="editable-cell-text-wrapper">
      {{ value || ' ' }}
      <a-icon type="edit" class="editable-cell-icon" @click="edit"/>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    text: Number
  },
  data() {
    return {
      value: this.text,
      editable: false
    };
  },
  methods: {
    handleChange(e) {
      const value = e.target.value;
      this.value = value;
    },
    check() {
      this.editable = false;
      this.$emit("change", this.value);
      this.editBranchesPrice(this.value);
    },
    edit() {
      this.editable = true;
    },

    // 编辑网点价格
    editBranchesPrice(price) {
      this.axios
        .post(this.baseUrl + "/goods/editBranchesPrice", {
          loginToken: this.$store.state.loginToken,
          id: this.$store.state.priceId,
          price: price
        })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.$message.info("改价成功");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    // 编辑网点价格
    editAgentGoods(price) {
      this.axios
        .post(this.baseUrl + "/goods/editAgentGoods", {
          loginToken: this.$store.state.loginToken,
          agencyName: this.$store.state.priceId,
          id: this.$store.state.priceId
        })
        .then(response => {
          console.log(response);
          if (response.data.code == 0) {
            this.$message.info("改价成功");
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

<style scoped>
.editable-cell .ant-input {
  width: 70px;
}
</style>

